using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class PhieuChuyenKhoDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SavePhieuChuyenKho(PhieuChuyenKho phieuchuyenkho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddPhieuChuyenKho", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddPhieuChuyenKho";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, phieuchuyenkho.Id);
			cmd.Parameters.Add(param);
            param = CreateParameter("@Prefix", DbType.String, ParameterDirection.Input, phieuchuyenkho.Prefix);
            cmd.Parameters.Add(param);
            param = CreateParameter("@OrderNumber", DbType.String, ParameterDirection.Input, phieuchuyenkho.OrderNumber);
            cmd.Parameters.Add(param);
            param = CreateParameter("@MaSo", DbType.String, ParameterDirection.Input, phieuchuyenkho.MaSo);
            cmd.Parameters.Add(param);
            param = CreateParameter("@History", DbType.String, ParameterDirection.Input, phieuchuyenkho.History);
            cmd.Parameters.Add(param);
			param = CreateParameter("@NgayThang", DbType.DateTime, ParameterDirection.Input, phieuchuyenkho.NgayThang);
			cmd.Parameters.Add(param);
			param = CreateParameter("@KhoXuat", DbType.String, ParameterDirection.Input, phieuchuyenkho.KhoXuat);
			cmd.Parameters.Add(param);
			param = CreateParameter("@KhoNhan", DbType.String, ParameterDirection.Input, phieuchuyenkho.KhoNhan);
			cmd.Parameters.Add(param);
			param = CreateParameter("@LyDo", DbType.String, ParameterDirection.Input, phieuchuyenkho.LyDo);
			cmd.Parameters.Add(param);
			param = CreateParameter("@NguoiLapPhieu", DbType.String, ParameterDirection.Input, phieuchuyenkho.NguoiLapPhieu);
			cmd.Parameters.Add(param);
            param = CreateParameter("@IsDelete", DbType.Int32, ParameterDirection.Input, phieuchuyenkho.IsDelete);
            cmd.Parameters.Add(param);
			param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
			cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();
			result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			
			return result;
		}
		
		// Get all PhieuChuyenKho in DB with IsDelete = 0
		public ResultObj GetAllPhieuChuyenKho( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<PhieuChuyenKho> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllPhieuChuyenKho", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<PhieuChuyenKho>();
			PhieuChuyenKho item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new PhieuChuyenKho();
			item.Id = Convert.ToInt32(reader["Id"]);
            item.Prefix = Convert.ToString(reader["Prefix"]);
            item.OrderNumber = Convert.ToString(reader["OrderNumber"]);
            item.MaSo = Convert.ToString(reader["MaSo"]);
            item.History = Convert.ToString(reader["History"]);
			item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
			item.KhoXuat = Convert.ToString(reader["KhoXuat"]);
			item.KhoNhan = Convert.ToString(reader["KhoNhan"]);
			item.LyDo = Convert.ToString(reader["LyDo"]);
			item.NguoiLapPhieu = Convert.ToString(reader["NguoiLapPhieu"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}

		// Get all PhieuChuyenKho in DB with condition
		public ResultObj GetAllPhieuChuyenKhoByID(PhieuChuyenKho phieuchuyenkho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<PhieuChuyenKho> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllPhieuChuyenKhoByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, phieuchuyenkho.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<PhieuChuyenKho>();
			PhieuChuyenKho item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new PhieuChuyenKho();
			item.Id = Convert.ToInt32(reader["Id"]);
            item.Prefix = Convert.ToString(reader["Prefix"]);
            item.OrderNumber = Convert.ToString(reader["OrderNumber"]);
            item.MaSo = Convert.ToString(reader["MaSo"]);
            item.History = Convert.ToString(reader["History"]);
			item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
			item.KhoXuat = Convert.ToString(reader["KhoXuat"]);
			item.KhoNhan = Convert.ToString(reader["KhoNhan"]);
			item.LyDo = Convert.ToString(reader["LyDo"]);
			item.NguoiLapPhieu = Convert.ToString(reader["NguoiLapPhieu"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Delete a PhieuChuyenKho out of DB with condition
		public ResultObj DeletePhieuChuyenKho(PhieuChuyenKho phieuchuyenkho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeletePhieuChuyenKho", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeletePhieuChuyenKho";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, phieuchuyenkho.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
		
		// Get info of CTPhieuChuyenKho from DB with condition
		public ResultObj GetCTPhieuChuyenKho(int IdCTPhieuChuyenKho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<CTPhieuChuyenKho> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetCTPhieuChuyenKhoByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdCTPhieuChuyenKho);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<CTPhieuChuyenKho>();
			CTPhieuChuyenKho item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new CTPhieuChuyenKho();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdPhieuChuyenKho = Convert.ToInt32(reader["IdPhieuChuyenKho"]);
			item.TenHang = Convert.ToString(reader["TenHang"]);
			item.MaHang = Convert.ToString(reader["MaHang"]);
			item.DonViTinh = Convert.ToString(reader["DonViTinh"]);
			item.SoLuong = Convert.ToInt64(reader["SoLuong"]);
			item.DonGia = Convert.ToInt64(reader["DonGia"]);
			item.ThanhTien = Convert.ToInt64(reader["ThanhTien"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
					
	}
}
